package com.ahpu.lyf.stack;

public class StackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack=new ArrayStack(5);
        arrayStack.printStack();
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(5);
        arrayStack.printStack();
        arrayStack.push(6);
        arrayStack.printStack();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.printStack();
        arrayStack.pop();
    }
}

class ArrayStack{
    //栈顶位置
    private Integer top=-1;
    //栈最大容量
    private Integer maxSize;
    //数组模拟栈
    private int[] stack;

    public ArrayStack(Integer maxSize) {
        this.maxSize = maxSize;
        stack=new int[this.maxSize];
    }

    public boolean isFull(){
        return top==maxSize-1;
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public void push(Integer data){
        if (isFull()){
            System.out.println("栈满，无法添加数据！");
            return;
        }
        stack[++top]=data;
    }

    public void pop(){
        if (isEmpty()){
            System.out.println("栈空，无数据！");
            return;
        }
        System.out.println("元素："+stack[top--]+"出栈！");
    }

    public void printStack(){
        if(isEmpty()){
            System.out.println("栈空，无数据！");
            return;
        }
        System.out.println("开始按照出栈顺序打印元素");
        for (int i = top; i >=0 ; i--) {
            System.out.println(stack[i]);
        }
    }
}
